/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChromeController.java
 *
 * Created on 2010-nov-30, 19:23:59
 */

package simplepushclient;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
import javax.swing.JOptionPane;

/**
 * @author Filip
 */
public class ChromeController extends javax.swing.JFrame {
    private PusherTray parent;
    private Sender sender;
    private HotkeyListener h;
    private boolean sendKeys=false;

    /** Creates new form ChromeController */
    public ChromeController(PusherTray parent) {
        this.parent = parent;
        this.sender = new Sender(parent.getIp());
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        openButton = new javax.swing.JButton();
        clickButton = new javax.swing.JToggleButton();
        closeTabButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        switchTabButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ChromeController");

        jTextField1.setText("http://");

        jLabel1.setText("Open Adress:");

        openButton.setText("Open");
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        clickButton.setText("Click");
        clickButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clickButtonActionPerformed(evt);
            }
        });

        closeTabButton.setText("Close Active Tab");
        closeTabButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeTabButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Close Chrome");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        refreshButton.setText("Refresh Tab");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        switchTabButton.setText("<- Switch Tab");
        switchTabButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                switchTabButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(clickButton, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                            .addComponent(switchTabButton, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(closeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(closeTabButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(refreshButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(openButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {closeTabButton, openButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(5, 5, 5)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(openButton)
                    .addComponent(closeTabButton)
                    .addComponent(switchTabButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(clickButton)
                    .addComponent(closeButton)
                    .addComponent(refreshButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void push(String s){
        sender.setData(s);
        sender.run();
    }

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
        if(jTextField1.getText().startsWith("http")){
            push(jTextField1.getText());
        }else{
            push("google:"+jTextField1.getText());
        }
    }//GEN-LAST:event_openButtonActionPerformed
    private void clickButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clickButtonActionPerformed
        push("nircmd: setcursor 100 100");
        push("nircmd: sendmouse left click");
    }//GEN-LAST:event_clickButtonActionPerformed
    private void closeTabButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeTabButtonActionPerformed
        push("nircmd: sendkeypress ctrl+w");
    }//GEN-LAST:event_closeTabButtonActionPerformed
    private void switchTabButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_switchTabButtonActionPerformed
        push("nircmd: sendkeypress ctrl+tab");
    }//GEN-LAST:event_switchTabButtonActionPerformed
    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        push("kill: chrome.exe");
    }//GEN-LAST:event_closeButtonActionPerformed
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        push("nircmd: sendkeypress F5");
    }//GEN-LAST:event_refreshButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton clickButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton closeTabButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton openButton;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton switchTabButton;
    // End of variables declaration//GEN-END:variables

    private void activateKeys() {
        JIntellitype j = parent.getJintellitype();
        if(j==null){
            JOptionPane.showMessageDialog(null, "JIntelliType is missing");
        }else{
            j.registerHotKey(25,0,0x25);//Left
            j.registerHotKey(27,0,0x27);//Rigth
            j.registerHotKey(26,0,0x26);//Up
            j.registerHotKey(28,0,0x28);//Down
            sendKeys=true;
            h = new HotkeyListener(){
                public void onHotKey(int i) {
                    switch(i) {
                        case 25:
                            push("nircmd: sendkeypress 0x25");//Left
                            break;
                        case 27:
                            push("nircmd: sendkeypress 0x27"); //Right
                            break;
                        case 26:
                            push("nircmd: sendkeypress 0x26"); //Up
                            break;
                        case 28:
                            push("nircmd: sendkeypress 0x28");//Down
                            break;
                    }
                }
            };
        }
    }

    private void deactivateKeys() {
        JIntellitype j = parent.getJintellitype();
        if(j==null){
            JOptionPane.showMessageDialog(null, "JIntelliType is missing");
        }else{
            j.unregisterHotKey(25);
            j.unregisterHotKey(27);
            j.unregisterHotKey(26);
            j.unregisterHotKey(28);
            j.removeHotKeyListener(h);
            sendKeys=false;
        }
    }

    @Override
    public void dispose() {
        if(sendKeys)
            deactivateKeys();
        super.dispose();
    }

}
